package com.kly.cashmall.module.order;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;


import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarFragment;
import com.kly.cashmall.bean.LoanTypeBean;
import com.kly.cashmall.module.home.home_tab.tab_other.ArgumentsUpdateControl;
import com.kly.cashmall.module.order.adapter.LoanPagerAdapter;
import com.kly.cashmall.services.UserInfoHelper;
import com.kly.cashmall.widget.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

public class LoanTypeFragment extends BaseBarFragment implements ArgumentsUpdateControl {

    private SlidingTabLayout mOrderTypeTab;
    private ViewPager mOrderPager;

    private LoanPagerAdapter mOrderPagerAdapter;

    private int mIndex;

    private List<LoanTypeBean> mOrderTypeBeans;
    private int position = 0;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_loan_type;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setActionBar(false);
        getIndex();
        initView();
    }

    private void getIndex() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mIndex = bundle.getInt("type");
        }
    }

    @Override
    protected void onPageInTop() {
        super.onPageInTop();
    }

    private void initView() {
        mOrderTypeTab = bindView(R.id.tab_order_type);
        mOrderPager = bindView(R.id.order_pager);
        mOrderPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                mIndex = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        setTabList();
    }

    @Override
    protected void loadData() {
    }

    private void setTabList() {
        List<LoanTypeBean> typeBeans = new ArrayList<>();
        LoanTypeBean orderTypeBeanAll = new LoanTypeBean();
        orderTypeBeanAll.desc = getActivity().getResources().getString(R.string.underway);
        orderTypeBeanAll.orderListName = "0";
        orderTypeBeanAll.orderListType = 0;
        typeBeans.add(orderTypeBeanAll);
        LoanTypeBean orderTypeBeanRepayment = new LoanTypeBean();
        orderTypeBeanRepayment.desc = getActivity().getResources().getString(R.string.completed);
        orderTypeBeanRepayment.orderListName = "1";
        orderTypeBeanRepayment.orderListType = 1;
        typeBeans.add(orderTypeBeanRepayment);

        this.mOrderTypeBeans = typeBeans;
        mOrderPagerAdapter = new LoanPagerAdapter(getChildFragmentManager(), mOrderTypeBeans);
        mOrderPager.setOffscreenPageLimit(2);
        mOrderPager.setAdapter(mOrderPagerAdapter);
        mOrderTypeTab.setViewPager(mOrderPager,new String[]{getActivity().getResources().getString(R.string.underway), getActivity().getResources().getString(R.string.completed)});
        mOrderPager.setCurrentItem(0);
        autoRefreshData(0);
        mOrderPager.setCurrentItem(position);
    }

    /**
     * 刷新当前页面
     */
    private void autoRefreshData(int index) {
        mOrderPager.setCurrentItem(mIndex);
        if (mOrderPagerAdapter != null) {
            LoanListFragment orderListFragment = mOrderPagerAdapter.getFragment(index);
            if (orderListFragment != null) {
                orderListFragment.compareToRefresh();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     */
    public int getPositionOfAllOrder(List<LoanTypeBean> orderTypeBeans, int type) {
        int size = orderTypeBeans.size();
        for (int i = 0; i < size; i++) {
            LoanTypeBean orderType = orderTypeBeans.get(i);
            if (orderType.orderListType == type) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void updateArguments(@Nullable Bundle bundle) {
        if (UserInfoHelper.getInstance().isLogin() && mOrderTypeTab != null) {
            if (bundle != null) {
                mIndex = bundle.getInt("type");
            }
            autoRefreshData(mOrderTypeTab.getCurrentTab());
        }
    }
}
